﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using ShootSearch.Indexers;
using Lucene.Net.Documents;
using ShootSearch.Util.Serializers;
using System.Diagnostics;
using ShootSearch.Jobs;

namespace ShootSearch.Tests.Indexers
{
    [TestFixture]
    public class LuceneIndexerTests
    {
        public static void Run()
        {
            LuceneIndexerTests obj = new LuceneIndexerTests();
            obj.ConfigTest();
        }

        [Test]
        public void ConfigTest()
        {
            LuceneIndexerConfig expected = new LuceneIndexerConfig();
            expected.InterestedAttributes.Add(new LuceneIndexerConfig.LuceneIndexerAttribute()
            {
                Name = "Name",
                AllowNull = true,
                Index = LuceneIndexerConfig.FieldIndex.NOT_ANALYZED,
                Store = LuceneIndexerConfig.FieldStore.YES,
                TermVector = LuceneIndexerConfig.FieldTermVector.WITH_OFFSETS
            });

            string xml = XmlFormatter.ToXml(expected);
            Debug.WriteLine(xml);
            LuceneIndexerConfig actual = XmlFormatter.FromXml(xml, expected.GetType()) as LuceneIndexerConfig;

            Assert.AreNotEqual(actual, null);
            Assert.AreNotEqual(actual.InterestedAttributes, null);
            Assert.AreEqual(1, actual.InterestedAttributes.Count);

            Assert.AreEqual(expected.InterestedAttributes[0].TermVector, actual.InterestedAttributes[0].TermVector);

        }

        [Test]
        public void JobConfigTest()
        {
            IndexBuilderJobConfig expected = new IndexBuilderJobConfig();
            expected.Indexer = new ShootSearch.Core.AssemblyWithConfig(typeof(LuceneIndexer));

            LuceneIndexerConfig lic = expected.Indexer.Config as LuceneIndexerConfig;
            lic.InterestedAttributes.Add(new LuceneIndexerConfig.LuceneIndexerAttribute()
            {
                Name = "Name",
                AllowNull = true,
                Index = LuceneIndexerConfig.FieldIndex.NOT_ANALYZED,
                Store = LuceneIndexerConfig.FieldStore.YES,
                TermVector = LuceneIndexerConfig.FieldTermVector.WITH_OFFSETS
            });

            string xml = XmlFormatter.ToXml(expected);
            Debug.WriteLine(xml);
            IndexBuilderJobConfig actual = XmlFormatter.FromXml(xml, expected.GetType()) as IndexBuilderJobConfig;

            Assert.AreNotEqual(null, actual);
            Assert.AreNotEqual(null, actual.Indexer);

        }
    }
}
